package com.trio.zjq.utils;


import org.apache.commons.codec.binary.Base64;

import java.io.UnsupportedEncodingException;

/**
 * 封装各种编码以及解码的工具类.
 *
 * @author 未绪~.
 * @date 2017/11/21-16:56.
 */
public final class EncodesUtils {

    private static final String DEFAULT_URL_ENCODING = "UTF-8";

    private EncodesUtils(){
        throw new AssertionError();
    }

    /**
     * Base64的编码
     */
    public static String encodeBase64(byte[] data){
        return new String(Base64.encodeBase64(data));
    }

    /**
     * Base64的编码
     */
    public static String encodeBase64(String data){

        try {
            return new String(Base64.encodeBase64(data.getBytes(DEFAULT_URL_ENCODING)));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * Base64的解码
     */
    public static byte[] decodeBase64(String data){
        return Base64.decodeBase64(data.getBytes());
    }

    /**
     * Base64解码
     */
    public static String decodeBase64String(String data){

        try {
            return new String(Base64.decodeBase64(data.getBytes()),DEFAULT_URL_ENCODING);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return "";
    }
}
